function parseQuery(url) {
    var queryObj = {};
    var reg = /[?&]([^=&#]+)=([^&#]*)/g;
    var querys = url.match(reg);
    if (querys) {
        for (var i in querys) {
            var query = querys[i].split('=');
            var key = query[0].substr(1),
                value = query[1];
            queryObj[key] ? queryObj[key] = [].concat(queryObj[key], value) : queryObj[key] = value;
        }
    }
    return queryObj;
}
let url = window.location.href
let id = parseQuery(url).couponId

let ul = document.querySelector('#details>ul') //优惠卷列表
let ul2 = document.querySelector('.windows>ul') //轮播图列表
let prev = document.querySelector('.left')
let next = document.querySelector('.right')

/* console.log(prev); */
/* console.log(id); */
ajax({
    url: `http://chst.vip:1234/api/getcouponproduct?couponid=${id}`

}).then(res => {
    let result = res.result
        /* console.log(result); */
    ul.innerHTML = createLi(result) //渲染优惠卷列表
        /* 点击列表显示轮播图 */
    let lbs = document.querySelectorAll('.clickLb')
    let i = 0
    lbs.forEach(item => {
        item.index = i
        i++
        /* 点级展开轮播图 */
        item.onclick = function() {
            /*   console.log(item.index); */
            LB.style.height = '100%'
            ul2.style.left = -this.index * lisW + 'px'
            let count = this.index;
            /* 下一页 */
            prev.onclick = function() {
                    count--
                    console.log(count);
                    if (count < 0) {
                        alert('已经是第一页了')
                        count = 0
                        return

                    }


                    ul2.style.left = -count * lisW + 'px'
                }
                /* 上一页 */
            next.onclick = function() {
                count++

                if (count >= lis.length) {
                    alert('已经是最后一页了')
                    count = lis.length - 1
                    return
                }

                ul2.style.left = -count * lisW + 'px'
            }
        }

    })
    ul2.innerHTML = createLi2(result) //渲染轮播图 图片
    let lis = document.querySelectorAll('.lis')
    let lisW = lis[0].offsetWidth
    ul2.style.width = lisW * lis.length + 'px'




})

/* 点击隐藏轮播 */
let LB = document.querySelector('#LB')
let X = document.querySelector('.X')
X.onclick = function() {
        LB.style.height = 0;
        LB.style.overflow = 'hidden'
    }
    /* 渲染优惠卷详情函数 */
function createLi(arr) {
    let html = '';

    arr.forEach(item => {

        html += ` <li class='clickLb'>
    <div class="left">${item.couponProductImg}</div>
    <div class="right">
        <span class="details">${item.couponProductName}</span>
        <span class="price">${item.couponProductPrice}</span>
        <span class="data">${item.couponProductTime}</span>
    </div>
</li>`
    });
    return html
}
/* 轮播图图片列表函数 */
function createLi2(arr) {
    let html = '';
    arr.forEach(item => {
        html += ` <li class='lis'>${item.couponProductImg}</li>`
    });
    return html
}